package com.vpen.client.heard;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.PongMessage;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

/**
 * 描述
 *
 * @author 韦鹏
 * @date 2022/11/24 23:10
 */
@Slf4j
public class HeardWebSocketHandler extends AbstractWebSocketHandler {
    /**
     * 接受服务器发回来的pong
     */
    @Override
    protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
        log.debug("{}:pong", session.getRemoteAddress());
    }

    /**
     * 接受服务器的文本消息
     */
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.debug("{}:{}", session.getRemoteAddress(), message.getPayload());
    }

    /**
     * 当断开连接后调用
     * 实现断线重连
     */
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        log.debug("与服务器断开连接，正在尝试重连....");
        HeardClient.reconnect();
    }
}
